package cheeese.and.crackers;

import java.io.Serializable;
import java.util.ArrayList;

/**
 * Class representing a specific users list of Tasks
 * 
 * @author Cheese and Crackers
 *
 */
public class TaskList implements Serializable {
	private static final long serialVersionUID = 7526472295622776141L;
	private ArrayList<Task> tasks;
	
	/**
	 * Default Constructor to initialize array list
	 */
	public TaskList(){
		tasks = new ArrayList<Task>();
	}
	
	/**
	 * Adds a task to the users list of tasks.
	 *
	 * @param task The task to be added to the array list of tasks
	 */
	public void addTask(Task task) throws Exception{
		if(getTask(task.getDisplayDescription()) == null){
			if(task.getDisplayDescription().equals("")){
				throw new Exception("Invalid Display Description");
			}
			if(task.getLocation().equals("")){
				throw new Exception("Invalid Location");
			}
			if(task.getDetails().equals("")){
				throw new Exception("Invalid Details");
			}
			tasks.add(task);
		}
		else{
			throw new Exception("Display Description Already Exists");
		}
	}
	
	/**
	 * Gets a specified task
	 * 
	 * @param displayDescription The displayDescription of task to be returned
	 */
	public Task getTask(String displayDescription){
		for(int i = 0; i<tasks.size(); i++){
			if(tasks.get(i).getDisplayDescription().equals(displayDescription)){
				return tasks.get(i);
			}
		}
		return null;
	}
	
	/**
	 * Gets String array Representation of task list
	 */
	public String[] getStringTasks(Filter filter){
		ArrayList<String> stringUsers = new ArrayList<String>();
		for(int i = 0; i<tasks.size(); i++){
			Task currentTask =tasks.get(i);
			if(filter.matches(currentTask)){
				stringUsers.add(currentTask.getDisplayDescription());
			}
		}
		return stringUsers.toArray(new String[stringUsers.size()]);
	}
}
